﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace nl.tno.ict.multitouch.wpfmultitouch.controls
{
    public class Scatterview : Canvas
    {
        private string _source;
        private Random _random;
        private int _maxZ;
        private List<MTImage> _imageList;

        public string Source { get { return _source; } set { _source = value ;} }
        
        public Scatterview()
        {
            this.MinWidth = 601;
            this.MinHeight = 601;
            _imageList = new List<MTImage>();
            _random = new Random();
            this.Loaded+=new System.Windows.RoutedEventHandler(Scatterview_Loaded);
            _maxZ = 1;
        }

        private void Scatterview_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (_source != null)
            {
                int top;
                int left;
                //= dir.GetFiles();

                String[] jpgfiles = GetFiles(_source, "*BMP|*bmp|*.JPEG|*.jpeg|*.jpg|*.gif", SearchOption.TopDirectoryOnly);

                foreach (String file in jpgfiles)
                {
                    MTImage myImage3 = new MTImage();
                    BitmapImage bi3 = new BitmapImage();
                    bi3.BeginInit();
                    bi3.UriSource = new Uri(file);
                    bi3.EndInit();


                    myImage3.Width = bi3.Width;
                    myImage3.Height = bi3.Height;

                    while (myImage3.Width > 600)
                    {
                        myImage3.Width /= 2;
                        myImage3.Height /= 2;
                    }

                    myImage3.Stretch = Stretch.Fill;
                    myImage3.Source = bi3;
                    myImage3.CanBeClicked = true;
                    myImage3.CanBeRotated = true;
                    myImage3.CanBeScaled = true;
                    myImage3.CanBeDragged = true;

                    TransformGroup tfg = new TransformGroup();
                    RotateTransform rotation = new RotateTransform(_random.Next(-90, 90));
                    ScaleTransform scale = new ScaleTransform(1.0, 1.0);
                    myImage3.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
                    tfg.Children.Add(rotation);
                    tfg.Children.Add(scale);
                    top = this._random.Next(1, (int)(this.MinHeight - myImage3.Height));
                    left = this._random.Next(1, (int)(this.MinWidth - myImage3.Width));
                    myImage3.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(myImage3_MouseLeftButtonDown);
                    myImage3.RenderTransform = tfg;
                    this.Children.Add(myImage3);
                    myImage3.OnTop = false;
                    myImage3.IsHitTestVisible = false;
                    Canvas.SetLeft(myImage3, top);
                    Canvas.SetTop(myImage3, left);
                    Canvas.SetZIndex(myImage3, _maxZ);
                    myImage3.MouseDoubleClick += new System.Windows.RoutedEventHandler(myImage3_MouseDoubleClick);
                    _maxZ++;
                }
                String[] movieFiles = GetFiles(_source, "*wmv|*avi", SearchOption.TopDirectoryOnly);
                foreach (String item in movieFiles)
	            {
		            MTMediaElement media = new MTMediaElement();
                    media.Source = new Uri(item);
                    media.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(media_MouseLeftButtonDown);
                    media.Height = 300;
                    media.Width = 300;
                    media.Loaded += new System.Windows.RoutedEventHandler(media_Loaded);
                    this.Children.Add(media);
                    media.LoadedBehavior = MediaState.Manual;
                    media.CanBeClicked = true;
                    media.CanBeDragged = true;
                    media.CanBeRotated = true;
                    media.CanBeScaled = true;
	            }

            }
        }

        void myImage3_MouseDoubleClick(object sender, System.Windows.RoutedEventArgs e)
        {
            //Console.WriteLine("Doubleclick!!!!!");
        }

        void myImage3_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _maxZ++;
            if (sender is MTImage)
                Canvas.SetZIndex((MTImage)sender, _maxZ);
            else if (sender is MediaElement)
                Canvas.SetZIndex((MediaElement)sender, _maxZ);
        }

        void media_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            ((MediaElement)sender).Stop();
        }

        void media_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((MediaElement)sender).LoadedBehavior != MediaState.Play)
            {
                ((MediaElement)sender).LoadedBehavior = MediaState.Manual;
                ((MediaElement)sender).Play();
                ((MediaElement)sender).LoadedBehavior = MediaState.Play;
            }
            else
            {
                ((MediaElement)sender).LoadedBehavior = MediaState.Manual;
                ((MediaElement)sender).Pause();
                ((MediaElement)sender).LoadedBehavior = MediaState.Pause;
            }
            _maxZ++;
            if (sender is MediaElement)
                Canvas.SetZIndex((MediaElement)sender, _maxZ);


        }

        public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
        {
            string[] searchPatterns = searchPattern.Split('|');
            List<string> files = new List<string>();
            foreach (string sp in searchPatterns)
                files.AddRange(System.IO.Directory.GetFiles(path, sp, searchOption));
            files.Sort();
            return files.ToArray();
        }



    }
}
